﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kelper.Infrastructure.Abstract;

namespace Kelper.Infrastructure
{
    public class SessionAccountManager : IAccountManager
    {
        private const string UsernameKey = "user";
        private HttpSessionStateBase session;

        public SessionAccountManager(HttpSessionStateBase session)
        {
            this.session = session;
        }

        public string CurrentUser { get { return (string)session[UsernameKey]; } }

        public void Login(string username)
        {
            session[UsernameKey] = username;
        }

        public void Logout()
        {
            session.Remove(UsernameKey);
        }

        public bool IsLoggedIn()
        {
            return IsLoggedIn(CurrentUser);
        }

        private bool IsLoggedIn(string username)
        {
            return username != null && (string)session[UsernameKey] == username;
        }
    }
}